package com.qiku.service.impl;

import com.qiku.dao.CourseMapper;
import com.qiku.domin.*;
import com.qiku.service.CourseService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;




    @Override
    public List<Course> findCourseByCondition(CourseVO courseVO) {
        return courseMapper.findCourseByCondition(courseVO);
    }



    @Override
    public void saveCourseOrTeacher(CourseVO courseVO) throws InvocationTargetException, IllegalAccessException {
        // 封装课程信息
        Course course = new Course();
        BeanUtils.copyProperties(course , courseVO);
        // 补全信息
        Date date = new Date();
        course.setCreateTime(date);
        course.setUpdateTime(date);
        // 保存课程信息
        courseMapper.saveCourse(course);
        // 获取新课程的id
        int courseId = course.getId();
        // 封装讲师信息
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacher , courseVO);
        // 补全信息
        teacher.setCourseId(courseId);
        teacher.setCreateTime(date);
        teacher.setUpdateTime(date);
        // 保存讲师信息
        courseMapper.saveTeacher(teacher);

    }

    @Override
    public void updateCourseOrTeacher(CourseVO courseVO) throws InvocationTargetException, IllegalAccessException {
        // 封装课程信息
        Course course = new Course();
        BeanUtils.copyProperties(course , courseVO);
        // 补全信息
        Date date = new Date();
        course.setUpdateTime(date);
        // 保存课程信息
        courseMapper.updateCourse(course);
        // 获取新课程的id
        int courseId = course.getId();
        // 封装讲师信息
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacher , courseVO);
        // 补全信息
        teacher.setCourseId(courseId);
//        teacher.setCreateTime(date);
        teacher.setUpdateTime(date);
        // 保存讲师信息
        courseMapper.updateTeacher(teacher);




    }

    @Override
    public CourseVO findCourseById(Integer id) throws InvocationTargetException, IllegalAccessException {
        Course course = courseMapper.findCourseById(id);
        CourseVO courseVO = new CourseVO();
        // 将course中的属性复制到 courseVO中
        BeanUtils.copyProperties(courseVO , course);
        return courseVO;
    }



    @Override
    public void updateStatusCourse(Integer id , Integer status) throws InvocationTargetException, IllegalAccessException {
        Course course = new Course();
        course.setId(id);
        course.setStatus(status);
//        course.setUpdateTime(new Date());
        courseMapper.updateCourseStatus(course);

    }

    @Override
    public List<CourseSection> findSectionAndLessonByCourseId(int courseId) {
        List<CourseSection> sectionList = courseMapper.findSectionAndLessonByCourseId(courseId);
        return sectionList;
    }

    @Override
    public Course findCourseByCourseId(int courseId) {
        return courseMapper.findCourseByCourseId(courseId);

    }

    @Override
    public void saveCourseSection(CourseSection courseSection) {
        Date date = new Date();
        courseSection.setCreateTime(date);
        courseSection.setUpdateTime(date);
        courseMapper.savecourseSection(courseSection);
    }

    @Override
    public void   updateCourseSection(CourseSection courseSection) {
        Date date = new Date();
//        courseSection.setCreateTime(date);
        courseSection.setUpdateTime(date);
        courseMapper.updatecourseSection(courseSection);
    }

    @Override
    public void updateStatusCourseSection(Integer id, int status) {
        CourseSection courseSection = new CourseSection();
        courseSection.setId(id);
        courseSection.setStatus(status);
        courseMapper.updateCourseSectionStatus(courseSection);

    }

    @Override
    public void saveLesson(CourseLesson courseLesson) {
        Date date = new Date();
        courseLesson.setCreateTime(date);
        courseLesson.setUpdateTime(date);
        courseMapper.saveLesson(courseLesson);
    }
}
